﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WCrawlerLib;
using System.Threading;
using System.Threading.Tasks;

namespace WCrawler
{
    public partial class frmForumThreads : System.Web.UI.Page
    {
        /// <summary>
        /// Biến dùng cho thể hiện số thứ tự của một forum
        /// </summary>
        private int _index = 0;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void gridViewForumThreads_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
                _index = gridViewForumThreads.PageSize * gridViewForumThreads.PageIndex + 1;
            else if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //Create order numbers
                ((Label)e.Row.FindControl("lblNum")).Text = (_index++).ToString();
                HyperLink hyperLinkThread = (HyperLink)e.Row.FindControl("hyperLinkThread");
                if (hyperLinkThread != null)
                {
                    CheckBox chkIsAlive = (CheckBox)e.Row.FindControl("chkIsAlive");
                    if (chkIsAlive.Checked)
                    {
                        hyperLinkThread.Style.Add("color", "#368a30");
                        hyperLinkThread.Style.Add("text-decoration", "none");
                    }
                    else //dead
                    {
                        hyperLinkThread.Style.Add("color", "#FF0000");
                        hyperLinkThread.Style.Add("text-decoration", "none");
                    }
                }
            }
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    ForumThread objForumThread = new ForumThread();
            //    objForumThread = objForumThread.GetForumThreads((int)gridViewForumThreads.DataKeys[e.Row.RowIndex]["ForumThreadId"]);
            //    HyperLink hyperLinkThread = (HyperLink)e.Row.FindControl("hyperLinkThread");
            //    hyperLinkThread.NavigateUrl = objForumThread.Forum.ForumUrl + "/" + objForumThread.ThreadUrl;
            //    hyperLinkThread.Text = objForumThread.ThreadTitle;
            //}
        }

        public void RunParallel(Forum objForum, List<int> indexList, int startIndex, int size)
        {
            //Run parallel
            Parallel.For(0, size, i =>
            {
                int rowIndex = indexList[i + startIndex];
                GridViewRow row = gridViewForumThreads.Rows[rowIndex];
                objForum.CheckForumThreads((int)gridViewForumThreads.DataKeys[row.RowIndex].Value);
            });

            //init paramaters after run parallel
            startIndex += size;
            if (startIndex + size > indexList.Count)
            {
                size = indexList.Count - startIndex;
                if (size > 0)
                    RunParallel(objForum, indexList, startIndex, size);
            }
            else
            {
                RunParallel(objForum, indexList, startIndex, size);
            }
        }


        protected void btnCheckThreads_Click(object sender, EventArgs e)
        {
            //Get selected rows group by forumId
            Dictionary<int, List<int>> forumThreadDict = new Dictionary<int, List<int>>();
            foreach (GridViewRow row in gridViewForumThreads.Rows)
            {
                //Get forumId and add into dictionary
                ForumThread objForumThread = new ForumThread();
                objForumThread = objForumThread.GetForumThread((int)gridViewForumThreads.DataKeys[row.RowIndex].Values[0]);
                if (!forumThreadDict.Keys.Contains(objForumThread.ForumId))
                    forumThreadDict.Add(objForumThread.ForumId, new List<int>());

                //add row index
                forumThreadDict[objForumThread.ForumId].Add(row.RowIndex);
            }

            //run by forum groups
            foreach (KeyValuePair<int, List<int>> threadList in forumThreadDict)
            {
                List<int> indexList = threadList.Value;
                //Run parallel with [size] forums every loop
                int startIndex = 0; //index of indexList
                int size = 20; //run maximum 50 forums in indexList for a parallel
                //Check if indexList < default size then update initialization
                if (indexList.Count < size)
                    size = indexList.Count;
                Forum objForum = new Forum();
                objForum = objForum.GetForum(threadList.Key);
                if (objForum.LoadForum() != null)
                {
                    //Start run parallel loops
                    RunParallel(objForum, indexList, startIndex, size);
                }
            }

            //Refresh gridview
            gridViewForumThreads.DataBind();
        }
    }
}